<!--
 * @Descripttion: 编辑轮播图
 * @Author: xiao li
 * @Date: 2020-09-28 15:24:24
 * @LastEditors: xiao li
 * @LastEditTime: 2021-07-17 16:39:11
-->
<template>
  <div class="lb-system-banner-edit">
    <top-nav :title="navTitle" :isBack="true" />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="130px"
      >
        <el-form-item label="图片" prop="img">
          <lb-cover
            :fileList="subForm.img"
            @selectedFiles="getCover"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸: 750 * 400</lb-tool-tips>
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            :min="0"
            :controls="false"
            v-model="subForm.top"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitForm">{{
            $t('action.submit')
          }}</lb-button>
          <lb-button @click="$router.back(-1)">{{
            $t('action.back')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      id: '',
      navTitle: '',
      articleOptions: [],
      subForm: {
        img: '',
        top: 0
      },
      subFormRules: {
        img: { required: true, type: 'array', message: '请上传图片', trigger: 'blur' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      }
    }
  },
  created () {
    let { id } = this.$route.query
    if (id) {
      this.subForm.id = id
      this.getDetail(id)
    }
    this.navTitle = this.$t(id ? 'menu.ServiceBannerEdit' : 'menu.ServiceBannerAdd')
  },
  methods: {
    /**
     * @method 获取图片
     */
    getCover (img) {
      this.subForm.img = img
    },
    async getDetail (id) {
      let { code, data } = await this.$api.service.bannerInfo({ id })
      if (code !== 200) return
      data.img = [{ url: data.img }]
      data.link = data.link * 1 > 0 ? data.link * 1 : ''
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    /**
     * @method 新增/编辑
     */
    submitForm () {
      let flag = true
      this.$refs['subForm'].validate(valid => {
        if (!valid) flag = false
      })
      if (flag) {
        let subForm = JSON.parse(JSON.stringify(this.subForm))
        subForm.img = subForm.img[0].url
        let modelMethod = subForm.id ? 'bannerUpdate' : 'bannerAdd'
        this.$api.service[modelMethod](subForm).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t(subForm.id ? 'tips.successRev' : 'tips.successSub'))
            this.$router.back(-1)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-banner-edit {
  width: 100%;
  .el-form {
    width: 100%;
    .el-form-item {
      margin-bottom: 24px;
      .el-select,
      .el-input-number,
      .el-input {
        width: 300px;
      }
    }
    .last-form-item {
      margin-top: 30px;
    }
    .item-tips {
      margin-left: 120px;
      margin-bottom: 24px;
      color: #999999;
    }
  }
}
</style>
